/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import blueprint4j.Constants;
import blueprint4j.db.*;
import blueprint4j.gui.db.*;
import blueprint4j.utils.*;


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.text.*;
import javax.swing.*;

public class ToolbarGroup extends JPanel implements ActionListener  {

	private static Color BUTTONBACK=Color.GRAY;//new Color(172,168,153);
	private JToolBar toolbar = null;
	private JPanel btn_panel=new JPanel(new FlowLayout(FlowLayout.CENTER,500,10));
	private Gui.WindowsButton buttons[] = null;
	private String panel_names[] = null;
	private JPanel main = new JPanel();
	private ToolBarInterface panels[] = null;
	private JLabel main_label;
	private CardLayout cards=new CardLayout();
	private JPanel center;
	private ToolBarInterface last_panel = null;

	private ToolGroupListener toolgroup_listener = null;
	private String descriptions[] = null;

	/**
	 * Icon [icon_name] will be displayed on the top of the toolbar
	 * toolPlacement will be the first default active panel
	 * Name will be displayed as the toolbars name
	 * Images are the image icons of the different panels
	 * Names are the names icons of the different panels
	 * p_panels, the actual panels
	 * toolgroup_listener, if a listener needs to be setup when a panel is selected
	 */
	public ToolbarGroup(String icon_name,int toolPlacement,String name,String images[],String names[],String descriptions[],ToolBarInterface p_panels[],ToolGroupListener toolgroup_listener,boolean add_clock)
	throws BindException,IOException {
		super(new BorderLayout());
		this.setAutoscrolls(true);
		this.descriptions = descriptions;
		String position=Settings.getString("gui.toolbar_position",BorderLayout.WEST);
		int orientation;
		if (BorderLayout.WEST.equals(position) || BorderLayout.WEST.equals(position)) {
			orientation=JToolBar.VERTICAL;
		} else if (BorderLayout.SOUTH.equals(position) || BorderLayout.NORTH.equals(position)) {
			orientation=JToolBar.HORIZONTAL;
		} else {
			Settings.setString("gui.toolbar_position",BorderLayout.WEST);
			position=Settings.getString("gui.toolbar_position",BorderLayout.WEST);
			orientation=JToolBar.VERTICAL;
			Settings.save();
		}
		main.setLayout(new BorderLayout());
		panels = p_panels;
		toolbar = new JToolBar(name,orientation);
		buttons = new Gui.WindowsButton[images.length];
		center=new JPanel(cards);
		panel_names = new String[buttons.length];
		
		btn_panel.setBackground(BUTTONBACK);
		btn_panel.setBorder(
		   BorderFactory.createCompoundBorder(
		      BorderFactory.createMatteBorder(5,3,3,3,btn_panel.getBackground()),
		      BorderFactory.createCompoundBorder(
		         BorderFactory.createCompoundBorder(
		            BorderFactory.createMatteBorder(0,0,1,1,Color.WHITE),
		            BorderFactory.createMatteBorder(1,1,0,0,new Color(172,168,153))
		         ),
		         BorderFactory.createCompoundBorder(
		            BorderFactory.createMatteBorder(1,0,0,0,new Color(113,111,100)),
		            BorderFactory.createMatteBorder(0,1,0,0,Color.BLACK)
		         )
		      )
		   )
		);
		btn_panel.setPreferredSize(new Dimension(90,100));
		btn_panel.setMaximumSize(new Dimension(90,1000));
		btn_panel.setMinimumSize(new Dimension(90,100));
		
		for (int i= 0;i < buttons.length;i++) {
			//buttons[i] = new AnimatingButton(names[i], new String[]{images[i]}, 1);
			buttons[i] = new Gui.WindowsButton(names[i],names[i], images[i],this);
			btn_panel.add(buttons[i]);
			center.add(panels[i].getComponent(),names[i]);
			panels[i].setToolbarGroup(this);
			//panel_names[i] = buttons[i].getText();
			panel_names[i] = names[i];
		}
		//toolbar.add(btn_panel);
		JPanel named=new JPanel(new BorderLayout());
		JPanel top=new JPanel(new BorderLayout());
		main_label=new JTitleLabel(descriptions[0]){
			           public void paint(Graphics g){
				           Graphics2D g2=(Graphics2D)g;
				           g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
				           super.paint(g);
			           }
		           };
		main_label.setFont(main_label.getFont().deriveFont(Font.ITALIC|Font.BOLD,40f));
		main_label.setForeground(Color.WHITE);
		top.add(main_label,BorderLayout.WEST);
		if (add_clock) {
			top.add(new Clock(true),BorderLayout.EAST);
		}
		top.setForeground(Color.WHITE);
		top.setBackground(new Color(38,34,97));
		top.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		if (icon_name != null) {
			JPanel icon_panel=new JPanel(new BorderLayout());
			JLabel icon=new JLabel();
			icon.setIcon(
					Gui.loadIconsFromResource(new String[]{icon_name})[0]
			);
			icon_panel.setOpaque(false);
			icon_panel.add(icon,BorderLayout.EAST);
			top.add(icon_panel,BorderLayout.CENTER);
		}

		named.add(top,BorderLayout.NORTH);
		named.add(center,BorderLayout.CENTER);
		main.add(named);
		add(btn_panel,BorderLayout.WEST);
		add(main,BorderLayout.CENTER);
		setPanel(toolPlacement);

		this.toolgroup_listener = toolgroup_listener;
	}

	public void setName(ToolBarInterface panel,String name) {
		for (int i =0;i < panels.length;i++) {
			if (panels[i] == panel) {
				//buttons[i].setLabel(name);
				buttons[i].setTitle(name);
			}
		}
	}

	public void setName(String name) {
		toolbar.setName(name);
	}

	public void setPanel(int panel) {
		if (last_panel != null) {
			last_panel.deactivate();
		}
		cards.show(center,panel_names[panel]);
		main_label.setText(descriptions[panel]);
		last_panel = panels[panel];
		last_panel.activate();
	}

	/*
	private void setPanel(JPanel panel) {
		for (int i =0;i < panels.length;i++) {
			if (panels[i] == panel) {
				setPanel(i);
			}
		}
	}*/
	
	public boolean isPanelShowing(JPanel panel) {
		return (last_panel == panel);
	}
	
	public void setPanel(ToolBarInterface panel) {
		for (int i =0;i < panels.length;i++) {
			System.out.println(panels[i] + " == " + panel);
			if (panels[i] == panel) {
				setPanel(i);
				return;
			}
		}
	}

	public void actionPerformed(ActionEvent event) {
		blueprint4j.gui.security.User.resetLoginTime();
		for (int i = 0;i < buttons.length;i++) {
			if (event.getSource() == buttons[i]) {
				if (last_panel != null && last_panel.save()) {
					setPanel(i);
					if (toolgroup_listener != null) {
						toolgroup_listener.toolGroupSelected(descriptions[i]);
					}
					break;
				}
			}
		}
	}

	public static interface ToolGroupListener {
		void toolGroupSelected(String group);
	}

	public static interface ToolBarInterface extends SaveableComponent {
		public void setToolbarGroup(ToolbarGroup group);
		public void activate();
		public void deactivate();
	}
}

